package com.hymall.service.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hymall.bean.MarketCoupon;
import com.hymall.bean.MarketCouponExample;
import com.hymall.bean.MarketCouponUser;
import com.hymall.bean.MarketCouponUserExample;
import com.hymall.bean.common.BasePageInfo;
import com.hymall.bean.common.CommonData;
import com.hymall.bean.vo.WxCouponVo;
import com.hymall.mapper.MarketCouponMapper;
import com.hymall.mapper.MarketCouponUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 长平居士 - rolfcui@outlook.com
 * @description 推广管理 -> 优惠券管理 服务类实现
 * @date 2022/7/17 10:17
 */
@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;

    /**
     * 重写list，按查看时间更新状态，返回实时优惠券信息
     *
     * @author: 叶熠彬
     * @date: 2022/7/21 11:03
     * @param: [name, type, status, info]
     * @return: com.hymall.bean.common.CommonData<com.hymall.bean.MarketCoupon>
     */
    @Override
    public CommonData<MarketCoupon> query2(String name, Short type, Short status, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit(), info.getSort() + " " + info.getOrder());
        MarketCouponExample example = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = example.createCriteria();
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (type != null) {
            criteria.andTypeEqualTo(type);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        Date nowTime = new Date();
        List<MarketCoupon> coupons = marketCouponMapper.selectByExample(example);
        for (MarketCoupon coupon : coupons) {
            if (coupon.getTimeType() == 1 && coupon.getEndTime().before(nowTime)) {
                coupon.setStatus((short) 1);
                marketCouponMapper.updateByPrimaryKey(coupon);
            }
        }

        PageInfo<MarketCoupon> pageInfo = new PageInfo<>(coupons);
        return CommonData.data(pageInfo);
    }


    /**
     * 增加了添加时间、更新时间
     *
     * @author: 叶熠彬
     * @date: 2022/7/21 10:03
     * @param: [marketCoupon]
     * @return: void
     */
    public int insert2(MarketCoupon marketCoupon) {
        Date date = new Date();
        marketCoupon.setAddTime(date);
        marketCoupon.setUpdateTime(date);
        marketCouponMapper.insertSelective(marketCoupon);
        return 0;
    }

    /**
     * 增加了更新时间和根据更改后信息调整状态码
     *
     * @author: 叶熠彬
     * @date: 2022/7/21 10:18
     * @param: [couponItem]
     * @return: int
     */
    @Override
    public int update2(MarketCoupon couponItem) {
        Date date = new Date();
        //优惠券重新生效
        if (couponItem.getStatus() == 1) {
            if (couponItem.getEndTime().after(date)) {
                couponItem.setStatus((short) 0);
            }
        }
        couponItem.setUpdateTime(date);
        marketCouponMapper.updateByPrimaryKey(couponItem);
        return 0;
    }

    @Override
    /**
     * @author 长平居士
     * @description 优惠券模式，显示所有优惠券
     * @date 2022/7/17 11:46
     *
     * @param name 优惠券名称
     * @param type 优惠券类型 三种
     * @param status 优惠券状态 三种
     * @param info
     * @return com.hymall.bean.common.CommonData<com.hymall.bean.MarketCoupon>
     */
    public CommonData<MarketCoupon> query(String name, Short type, Short status, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketCouponExample example = new MarketCouponExample();
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketCouponExample.Criteria criteria = example.createCriteria();
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (type != null) {
            criteria.andTypeEqualTo(type);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }

        List<MarketCoupon> coupons = marketCouponMapper.selectByExample(example);
        PageInfo<MarketCoupon> pageInfo = new PageInfo<>(coupons);
        return CommonData.data(pageInfo);
    }

    @Override
    /**
     * @author 长平居士
     * @description 优惠券模式，新增优惠券。
     * @date 2022/7/18 12:31
     *
     * @param marketCoupon
     * @return void
     */
    public void insert(MarketCoupon marketCoupon) {
        marketCouponMapper.insert(marketCoupon);
    }

    @Override
    /**
     * @author 长平居士
     * @description 优惠券模式，修改优惠券
     * @date 2022/7/18 15:33
     *
     * @param marketCoupon
     * @return void
     */
    public void update(MarketCoupon marketCoupon) {
        marketCouponMapper.updateByPrimaryKey(marketCoupon);
    }

    @Override
    /**
     * @author 长平居士
     * @description 优惠券模式，删除优惠券
     * @date 2022/7/18 15:33
     *
     * @param couponItem
     * @return void
     */
    public void delete(MarketCoupon couponItem) {
        marketCouponMapper.deleteByPrimaryKey(couponItem.getId());
    }

    @Override
    /**
     * @author 长平居士
     * @description 优惠券模式，读取详细信息
     * @date 2022/7/18 15:34
     *
     * @param id
     * @return com.hymall.bean.MarketCoupon
     */
    public MarketCoupon read(Integer id) {
        MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(id);
        return marketCoupon;
    }

    @Override
    /**
     * @author 长平居士
     * @description 优惠券模式，使用者列表
     * @date 2022/7/18 15:34
     *
     * @param couponId
     * @param info
     * @return com.hymall.bean.common.CommonData<com.hymall.bean.MarketCouponUser>
     */
    public CommonData<MarketCouponUser> listuser(Integer couponId, Integer userId, Short status, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit(), info.getSort() + " " + info.getOrder());

        MarketCouponUserExample example = new MarketCouponUserExample();
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketCouponUserExample.Criteria criteria = example.createCriteria();
        criteria.andCouponIdEqualTo(couponId);
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }

        List<MarketCouponUser> couponUsers = marketCouponUserMapper.selectByExample(example);
        PageInfo<MarketCouponUser> pageInfo = new PageInfo<>(couponUsers);
        return CommonData.data(pageInfo);
    }


}
